package com.spring.webclient;

import com.spring.webclient.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.concurrent.atomic.AtomicInteger;

@SpringBootApplication
@Slf4j
public class SpringTask3WebclientApplication implements ApplicationRunner {

	@Autowired
	private WebClient webClient;

	public static void main(String[] args) {
		new SpringApplicationBuilder(SpringTask3WebclientApplication.class)
				.web(WebApplicationType.NONE)
				.bannerMode(Banner.Mode.OFF)
				.run(args);
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		AtomicInteger integer = new AtomicInteger(0);
		webClient.get()
				.uri("/coffee/getAllForJson")
				.retrieve()
				.bodyToFlux(Coffee.class)
				.toStream()
				.forEach(c -> log.info("第{}行数据，打印是: {}",integer.incrementAndGet(), c));
	}
}
